import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.*;


public class Start extends JFrame {
	
	private JButton create;
	
	private JButton join;
	
	private JPanel panel;
	
	private JTextField text;
	
	private JLabel ip;
	
	private JButton start;
	
	private JPanel joinPanel;
	
	public Start(){
		setLayout(new GridLayout(2, 1));
		create = new JButton("CREATE GAME");
		join = new JButton("JOIN GAME"); 
		panel = new JPanel();
		text = new JTextField();
		ip = new JLabel("Input IP: ");
		start = new JButton("START");
		joinPanel = new JPanel();
		GameBoard gb = new GameBoard();
		Othello o = new Othello(gb);
		//System.out.println("start");
		initcomponent();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		run();
	}
	private void run() {
	
		this.setVisible(true);	
	}
	private void initcomponent() {
		panel.add(ip);
		panel.add(text);
		text.setPreferredSize(new Dimension(80,20));
		joinPanel.setLayout(new FlowLayout());
		joinPanel.add(ip);
		joinPanel.add(text);
		//joinPanel.add(start);
		add(create);
		create.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
					GameBoard gb = new GameBoard();
					Othello o = new Othello(gb);
					Server server = new Server(45001,o);
					try {
						server.listen();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					//System.out.println("Server start");
					setVisible(false);
					Client client = new Client("127.0.0.1", 45001);
					OthelloUI oUI = new OthelloUI(client, o);
					ClientUI ui = new ClientUI(client, oUI);
					client.setUI(oUI);
					try {
						client.openConnection();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		add(join);
		join.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				create.setVisible(false);
				join.setVisible(false);
				add(joinPanel);
				add(start);
				remove(create);
				remove(join);
				pack();
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		start.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				setVisible(false);
				String host = text.getText();
				Client client = new Client(host, 45001);
				GameBoard gb = new GameBoard();
				Othello o = new Othello(gb);
				OthelloUI oUI = new OthelloUI(client, o);
				
				client.setUI(oUI);
				
				ClientUI ui = new ClientUI(client, oUI);

				gb.addObserver(oUI);
					try {
						client.openConnection();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					//System.out.println("client connect");
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	public static void main(String[] args) {
		Start begin = new Start();
	}
}
